﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Course;
using Telerik.WebControls;

public partial class Theme_DL_EditUser : PowerCreator.LMS.Web.Base.BasePage
{
    TOLearner obj = new TOLearner();
    Course opCourse = new Course();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";

    PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        //this.Title = config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page);
        if (!IsPostBack)
        {
            InitPage();
            
            BinderRegion();
            BinderSchool(int.Parse(ddl_Region.SelectedValue));
            BinderSchoolStage();
            BinderCatalog();
            BinderXueNian();
            BinderJob();
            BinderDuty();
            BinderXueLi();
            BinderSepcial();

            BinderPage();
        }
    }
    //绑定区域
    private void BinderRegion()
    {
        TORegion obj = new TORegion();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_Region.DataSource = dt;
        ddl_Region.DataTextField = "Region";
        ddl_Region.DataValueField = "ID";
        ddl_Region.DataBind();

    }

    //绑定学校
    private void BinderSchool(int RegionID)
    {
        TOSchoolGroup obj = new TOSchoolGroup();
        string search = "1=1";
        if (RegionID.ToString() != "0")
        {
            VOTORegion voRegion = new VOTORegion();
            TORegion objRegion = new TORegion();
            voRegion = objRegion.Load(RegionID);
            search += " and SchoolGroupRegion='" + voRegion.Region + "'";
        }

        DataTable dt = obj.Query(search);

        ddl_School.DataSource = dt;
        ddl_School.DataTextField = "SchoolGroupName";
        ddl_School.DataValueField = "SchoolGroup_Id";
        ddl_School.DataBind();


    }


    //绑定学段
    private void BinderSchoolStage()
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_xueduan_Stage.DataSource = dt;
        ddl_xueduan_Stage.DataTextField = "SchoolStage";
        ddl_xueduan_Stage.DataValueField = "ID";
        ddl_xueduan_Stage.DataBind();

    }

    //绑定学科
    private void BinderCatalog()
    {
        Catalog obj = new Catalog();
        string search = "";
        DataTable dt = obj.QueryCatalog(search);

        ddl_xeke_Catalog.DataSource = dt;
        ddl_xeke_Catalog.DataTextField = "CatalogName";
        ddl_xeke_Catalog.DataValueField = "CatalogId";
        ddl_xeke_Catalog.DataBind();

    }

    //绑定学年
    private void BinderXueNian()
    {
        ListItem li1 = new ListItem();
        li1.Text = "小学低年级";
        li1.Value = "1";
        ddl_xuenian.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "小学高年级";
        li2.Value = "2";
        ddl_xuenian.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "初一年级";
        li3.Value = "3";
        ddl_xuenian.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "初二年级";
        li4.Value = "4";
        ddl_xuenian.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "初三年级";
        li5.Value = "5";
        ddl_xuenian.Items.Add(li5);

        ListItem li6 = new ListItem();
        li6.Text = "高一年级";
        li6.Value = "6";
        ddl_xuenian.Items.Add(li6);
        ListItem li7 = new ListItem();
        li7.Text = "高二年级";
        li7.Value = "7";
        ddl_xuenian.Items.Add(li7);
        ListItem li8 = new ListItem();
        li8.Text = "高三年级";
        li8.Value = "8";
        ddl_xuenian.Items.Add(li8);
    }
    //绑定职务
    private void BinderJob()
    {
        Job obj = new Job();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_zhiwu.DataSource = dt;
        ddl_zhiwu.DataTextField = "TeajobName";
        ddl_zhiwu.DataValueField = "TeajobId";
        ddl_zhiwu.DataBind();

    }
    //绑定职称
    private void BinderDuty()
    {
        Duty obj = new Duty();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_zhicheng.DataSource = dt;
        ddl_zhicheng.DataTextField = "TeadutyName";
        ddl_zhicheng.DataValueField = "TeadutyId";
        ddl_zhicheng.DataBind();

    }
    //绑定当前学历
    private void BinderXueLi()
    {
        ListItem li1 = new ListItem();
        li1.Text = "中专";
        li1.Value = "1";
        ddl_xueli.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "大专";
        li2.Value = "2";
        ddl_xueli.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "本科";
        li3.Value = "3";
        ddl_xueli.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "硕士";
        li4.Value = "4";
        ddl_xueli.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "博士";
        li5.Value = "5";
        ddl_xueli.Items.Add(li5);

        ListItem li6 = new ListItem();
        li6.Text = "高中及以下";
        li6.Value = "6";
        ddl_xueli.Items.Add(li6);


    }
    //绑定特殊分类
    private void BinderSepcial()
    {
        ListItem li1 = new ListItem();
        li1.Text = "班主任";
        li1.Value = "1";
        ddl_Sepcial.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "外语教师";
        li2.Value = "2";
        ddl_Sepcial.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "新上岗教师";
        li3.Value = "3";
        ddl_Sepcial.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "市骨干";
        li4.Value = "4";
        ddl_Sepcial.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "一般教师";
        li5.Value = "5";
        ddl_Sepcial.Items.Add(li5);

        ListItem li6 = new ListItem();
        li6.Text = "特级教师";
        li6.Value = "6";
        ddl_Sepcial.Items.Add(li6);


    }
    protected void ddl_Region_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderSchool(int.Parse(ddl_Region.SelectedValue));
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }
    protected  void InitPage()
    {

        //lbl_InfoTitle_AddStudent.Text = config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_PassWord.Text = config.GetLanagerValueByKey("lbl_AddStudent_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);

        //lbl_dbPassWord.Text = config.GetLanagerValueByKey("lbl_AddStudent_DBPassWord", PowerCreator.LMS.Language.LanagetItemType.Page);

        //lbl_addstudent_selClass.Text = config.GetLanagerValueByKey("lbl_addstudent_selClass", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_AddNew.Text = config.GetLanagerValueByKey("btn_EditNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = config.GetLanagerValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

       
    }

    private void BinderPage()
    {
        try
        {
            txt_UserName.Text = GetLoginEntity.UserName;
            int uid = GetLoginEntity.UserID;
            TOLearner obj = new TOLearner();
            //if (GetLoginEntity.AccountType == PowerCreator.LMS.Web.User.LoginUserType.Student)
            //{
            VOTOLearner vo = new VOTOLearner();
            vo = obj.Load(uid);

                txt_RealName.Text = vo.Name;
               
                if (!vo.Sex )
                    radbtn_Male.Checked = true;
                else
                    radbtn_Female.Checked = true;
               
                txt_Birth.Text = vo.Birthday.ToShortDateString();
                txt_Email.Text = vo.Email;
                txt_Tel.Text = vo.Mobile;
                
                
                ddl_Region.SelectedValue = vo.Region.ToString();
                ddl_School.SelectedValue = vo.School.ToString();
                ddl_Sepcial.SelectedValue = vo.Sepcial.ToString();
                ddl_xeke_Catalog.SelectedValue = vo.XueKe.ToString();
                ddl_xueduan_Stage.SelectedValue = vo.XueDuan.ToString();
                ddl_xueli.SelectedValue = vo.Education.ToString();
                ddl_xuenian.SelectedValue = vo.XueNian.ToString();
                ddl_zhicheng.SelectedValue = vo.ZhiCheng.ToString();
                ddl_zhiwu.SelectedValue = vo.ZhiWu.ToString();
               
        }
        catch (Exception ex)
        {
            return;
        }
    }
    /// <summary>
    /// 检查页面数据
    /// </summary>
    /// <returns></returns>
    private bool CheckPageData()
    {
       

        if (!string.IsNullOrEmpty(txt_Birth.Text))
        {
            if (!TimeUtils.IsDateTime(txt_Birth.Text.Trim()))
            {
                lab_Birth_css.Text = config.GetLanagerValueByKey("theme_Register_Msg3", PowerCreator.LMS.Language.LanagetItemType.Page);
                return false;
            }
        }
        try
        {
           int XueKe = int.Parse(ddl_xeke_Catalog.SelectedValue);
        }
        catch
        {
            lbl_err.Text = "必须选择学校，请检查！";
            return false;
        }
        return true;
    }


 

    protected void btnAdd_Click(object sender, EventArgs e)
    {

        if (!CheckPageData()) return;
        int uid = GetLoginEntity.UserID;
        TOLearner obj = new TOLearner();
        ////PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
        //if (GetLoginEntity.AccountType == PowerCreator.LMS.Web.User.LoginUserType.Student)
        //{
        VOTOLearner vo = new VOTOLearner();
        vo = obj.Load(uid);


            vo.Name=txt_RealName.Text;
            if (radbtn_Male.Checked == true)
                vo.Sex= false;
            else
                vo.Sex = true;
            //vo.StudentIdentityCard = txt_Idcard.Text;
            if (!string.IsNullOrEmpty(txt_Birth.Text))
                vo.Birthday = DateTime.Parse(txt_Birth.Text.Trim());
            vo.Email = txt_Email.Text;
            vo.Mobile = txt_Tel.Text.Trim();
            if (radbtn_Female.Checked == true)
                vo.Sex = true;
            if (radbtn_Male.Checked == true)
                vo.Sex = false;

            //学校不能为空
            if (string.IsNullOrEmpty(ddl_School.SelectedValue))
            {
                string url = "EditUser.aspx";
                Script.ShowAndRedirect(this, "请现在该区域中添加学校，否则不能保存！", url);
                return;
            }
            vo.Birthday = DateTime.Parse(txt_Birth.Text.Trim());
            vo.Region = int.Parse(ddl_Region.SelectedValue);
            vo.School = int.Parse(ddl_School.SelectedValue);
            vo.XueDuan = int.Parse(ddl_xueduan_Stage.SelectedValue);
            vo.XueKe = int.Parse(ddl_xeke_Catalog.SelectedValue);
            vo.XueNian = int.Parse(ddl_xuenian.SelectedValue);
            vo.ZhiCheng = int.Parse(ddl_zhicheng.SelectedValue);
            vo.ZhiWu = int.Parse(ddl_zhiwu.SelectedValue);
            vo.Education = int.Parse(ddl_xueli.SelectedValue);
            vo.Sepcial = int.Parse(ddl_Sepcial.SelectedValue);

            
            if (obj.Update(vo))
            {
                string url = "StudyCenter.aspx";
                Script.ShowAndRedirect(this, "修改成功！", url);
                //Script.RedirectURL(url);
            }


    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "Default.aspx";
        Script.RedirectURL(url);
    }

}
